
package miui.xmpp.message;

import android.net.Uri;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Iq {

    public enum Type {
        Undefined, Set, Get, Result, Error;

        public static String toString(Type type) {
            String str = null;

            switch (type) {
                case Error:
                    str = "error";
                    break;

                case Get:
                    str = "get";
                    break;

                case Result:
                    str = "result";
                    break;

                case Set:
                    str = "set";
                    break;

                default:
                    str = "undefined";
                    break;
            }

            return str;
        }

        public static Type toType(String type) {
            Type t = Type.Undefined;

            if (type.equalsIgnoreCase("set")) {
                t = Type.Set;
            } else if (type.equalsIgnoreCase("get")) {
                t = Type.Get;
            } else if (type.equalsIgnoreCase("result")) {
                t = Type.Result;
            } else if (type.equalsIgnoreCase("error")) {
                t = Type.Error;
            }

            return t;
        }
    }

    private Type mType = Type.Undefined;
    private String mId = null;
    private String mXmlns = null;
    private String mAction = null;
    private String mParam = null;
    private String mFrom = null;

    public static Iq create(byte bytes[]) {
        if (bytes == null)
            return null;

        Iq iq = new Iq();
        if (!iq.load(bytes))
            return null;

        return iq;
    }

    public Iq() {
        mType = Type.Undefined;
    }

    public Iq(Type type, String id, String xmlns, String action, String param) {
        create(type, id, xmlns, action, param);
    }

    public Iq(Type type, int id, String xmlns, String action, String param) {
        String idStr = String.format("%d", id);
        create(type, idStr, xmlns, action, param);
    }

    public void create(Type type, String id, String xmlns, String action, String param) {
        mType = type;
        mId = id;
        mXmlns = xmlns;
        mAction = action;
        mParam = param;
    }

    public Type getType() {
        return mType;
    }

    public void setType(Type type) {
        mType = type;
    }

    public String getId() {
        return mId;
    }

    public void setId(String id) {
        mId = id;
    }

    public String getXmlns() {
        return mXmlns;
    }

    public void setXmlns(String xmlns) {
        mXmlns = xmlns;
    }

    public String getAction() {
        return mAction;
    }

    public void setAction(String action) {
        mAction = action;
    }

    public void setFrom(String from) {
        mFrom = from;
    }

    public String getFrom() {
        return mFrom;
    }

    public String getParam() {
        return mParam;
    }

    public void setParam(String param) {
        mParam = param;
    }

    public boolean load(byte bytes[]) {
        boolean result = false;

        do {
            if (bytes == null)
                break;

            try {
                JSONObject json = new JSONObject(new String(bytes));
                if (json == null) {
                    break;
                }

                json = json.optJSONObject("iq");
                if (json == null) {
                    break;
                }
                String type = json.optString("type");
                if (TextUtils.isEmpty(type)) {
                    break;
                }

                mType = Type.toType(type);

                mId = json.optString("id");

                mXmlns = json.optString("xmlns");
                if (mXmlns == null) {
                    break;
                }

                mAction = json.optString("action");
                if (TextUtils.isEmpty(mAction)) {
                    break;
                }

                mFrom = json.optString("from");

                String param = json.optString("param");
                if (!TextUtils.isEmpty(param)) {
                    mParam = Uri.decode(param);
                }
                result = true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (false);

        return result;
    }

    private Element getTag(Element node, String tag) {
        if (node == null)
            return null;

        NodeList tags = node.getElementsByTagName("*");
        for (int i = 0; i < tags.getLength(); ++i) {
            Element child = (Element) tags.item(i);
            if (child.getTagName().equalsIgnoreCase(tag)) {
                return child;
            }
        }

        return null;
    }

    @Override
    public String toString() {
        if (mType == Type.Undefined)
            return null;

        JSONObject json = new JSONObject();
        JSONObject iq = new JSONObject();
        try {
            json.put("type", Type.toString(mType));
            json.put("id", mId);
            json.put("xmlns", mXmlns);
            json.put("action", mAction);
            if (!TextUtils.isEmpty(mParam)) {
                String encoding = Uri.encode(mParam);
                json.put("param", encoding);
            }
            if (!TextUtils.isEmpty(mFrom)) {
                json.put("from", mFrom);
            }
            iq.put("iq", json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return iq.toString();
    }
}